home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / basic.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  21.8 KB  |  799 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  basic.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_BASIC_H
  16. #define LEDA_BASIC_H
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21.  
  22. //------------------------------------------------------------------------------
  23. // compiler/system dependent definitions and includes
  24. //------------------------------------------------------------------------------
  25.  
  26. #if defined(_SGI_) || defined(__sgi)
  27. /* random() declared in math.h */
  28. #include <math.h>
  29. #endif
  30.  
  31.  
  32. #if defined(__GNUG__)
  33. /* no random() function in libg++ (since version 2.5) */
  34. #define __NO_RANDOM__
  35. #if __GNUC_MINOR__ > 5
  36. #define __BUILTIN_BOOL__
  37. #endif
  38. #endif
  39.  
  40.  
  41. #if defined(__ZTC__)
  42. #define __MSDOS__
  43. #define __NO_RANDOM__
  44. #define MAXINT          (int(0x7FFFFFFF))
  45. #define MAXFLOAT    (float(3.37E+38))
  46. #define MAXDOUBLE       (double(1.797693E+308))
  47. #include <iostream.hpp>
  48. #else
  49. #include <values.h>
  50. #include <iostream.h>
  51. #endif
  52.  
  53.  
  54. #if defined(__TURBOC__)
  55. #define __NO_RANDOM__
  56. #define FRIEND_INLINE friend
  57. #else
  58. #define _CLASSTYPE
  59. #define FRIEND_INLINE friend inline
  60. #endif
  61.  
  62.  
  63.  
  64. //------------------------------------------------------------------------------
  65. // Global Types
  66. //------------------------------------------------------------------------------
  67.  
  68. typedef void* GenPtr;    // generic pointer type
  69.  
  70. typedef int  (*CMP_PTR)(GenPtr&,GenPtr&);
  71. typedef void (*APP_PTR)(GenPtr&);
  72. typedef int  (*ORD_PTR)(GenPtr&);
  73.  
  74.  
  75.  
  76. //------------------------------------------------------------------------------
  77. // Error Handling
  78. //------------------------------------------------------------------------------
  79.  
  80. typedef void (*PEH)(int,const char*);   // Pointer to Error Handler
  81.  
  82. extern PEH  p_error_handler;
  83. extern PEH  set_error_handler(PEH);
  84. extern void default_error_handler(int,const char*);
  85. inline void error_handler(int i, const char* s)  { p_error_handler(i,s); }
  86.  
  87.  
  88. //------------------------------------------------------------------------------
  89. // Templates
  90. //------------------------------------------------------------------------------
  91.  
  92. #include <LEDA/template.h>
  93.  
  94.  
  95. //------------------------------------------------------------------------------
  96. // LEDA INIT
  97. //------------------------------------------------------------------------------
  98.  
  99.  
  100. struct LEDA {
  101.  
  102.  LEDA();
  103. ~LEDA();
  104.  
  105. char* init_list;
  106. static const char* version_string;
  107. static int loop_dummy;
  108. };
  109.  
  110. extern LEDA L_E_D_A;
  111.  
  112.  
  113. //------------------------------------------------------------------------------
  114. // Memory Management
  115. //------------------------------------------------------------------------------
  116.  
  117. struct  memory_elem_type { memory_elem_type* next; };
  118. typedef memory_elem_type* memory_elem_ptr;
  119.  
  120. extern memory_elem_ptr memory_free_list[];
  121.  
  122. extern memory_elem_ptr memory_allocate_block(int);
  123. extern memory_elem_ptr allocate_bytes(int);
  124. extern memory_elem_ptr allocate_bytes_with_check(int);
  125. extern memory_elem_ptr allocate_words(int);
  126.  
  127. extern void deallocate_bytes(void*,int);
  128. extern void deallocate_bytes_with_check(void*,int);
  129. extern void deallocate_words(void*,int);
  130. extern void memory_clear();
  131. extern void memory_kill();
  132. extern void print_statistics();
  133.  
  134. extern int used_memory();
  135.  
  136. inline void deallocate_list(void* head,void* tail, int bytes)
  137. { memory_elem_ptr(tail)->next = memory_free_list[bytes];
  138.   memory_free_list[bytes] = memory_elem_ptr(head);
  139.  }
  140.  
  141. #define OPERATOR_NEW(bytes)\
  142. void* operator new(size_t)\
  143. { memory_elem_ptr* q = memory_free_list+bytes;\
  144.   if (*q==0) *q = memory_allocate_block(bytes);\
  145.   memory_elem_ptr p = *q;\
  146.   *q = p->next;\
  147.   return p;\
  148.  }
  149.  
  150. #define OPERATOR_DEL(bytes)\
  151. void  operator delete(void* p)\
  152. { memory_elem_ptr* q = memory_free_list+bytes;\
  153.   memory_elem_ptr(p)->next = *q;\
  154.   *q = memory_elem_ptr(p);\
  155.  }
  156.  
  157. #define OPERATOR_NEW_WITH_CHECK(bytes)\
  158. void* operator new(size_t) { return allocate_bytes_with_check(bytes); }
  159.  
  160. #define OPERATOR_DEL_WITH_CHECK(bytes)\
  161. void  operator delete(void* p) { deallocate_bytes_with_check(p,bytes); }
  162.  
  163.  
  164. #define LEDA_MEMORY(type)\
  165. OPERATOR_NEW(sizeof(type))\
  166. OPERATOR_DEL(sizeof(type))
  167.  
  168. #define LEDA_MEMORY_WITH_CHECK(type)\
  169. OPERATOR_NEW_WITH_CHECK(sizeof(type))\
  170. OPERATOR_DEL_WITH_CHECK(sizeof(type))
  171.  
  172.  
  173. //------------------------------------------------------------------------------
  174. // handle_base/rep: base classes for handle types string, point, segment,...
  175. //------------------------------------------------------------------------------
  176.  
  177.  
  178. class handle_rep  {
  179.  
  180. friend class handle_base;
  181.   
  182. protected:
  183.  
  184.    int  count;
  185.    
  186.          handle_rep()  { count = 1; }
  187. virtual ~handle_rep()  {}
  188.  
  189. LEDA_MEMORY(handle_rep);
  190.   
  191. };
  192.  
  193.  
  194. class handle_base {
  195.  
  196. protected:
  197.  
  198. handle_rep* PTR;
  199.  
  200. public:
  201.  
  202.  handle_base()                     {}
  203.  handle_base(const handle_base& x) { PTR = x.PTR;  PTR->count++; }
  204.  
  205. ~handle_base()                     {}
  206.  
  207.  
  208. handle_base& operator=(const handle_base& x)
  209. { x.PTR->count++;
  210.   if (--PTR->count == 0)  delete PTR;
  211.   PTR = x.PTR;
  212.   return *this;
  213.  }
  214.  
  215. public:
  216.  
  217. void clear()        { if (--(PTR->count)==0) delete PTR; }
  218. GenPtr copy() const { PTR->count++; return PTR; }
  219.  
  220.  
  221. FRIEND_INLINE int    compare(const handle_base&, const handle_base&);
  222. FRIEND_INLINE void   Print(const handle_base&, ostream& = cout);
  223. FRIEND_INLINE void   Read(handle_base&, istream& = cin);
  224. FRIEND_INLINE void   Init(handle_base&);
  225. FRIEND_INLINE void   Clear(handle_base&);
  226. FRIEND_INLINE GenPtr Copy(handle_base&);
  227. FRIEND_INLINE GenPtr Convert(const handle_base&);
  228.  
  229. };
  230.  
  231. inline int compare(const handle_base&, const handle_base&)
  232. { error_handler(1,"compare undefined"); 
  233.   return 0; }
  234.  
  235. inline void Print(const handle_base&, ostream&)
  236. { error_handler(1,"Print undefined");  }
  237.  
  238. inline void Read(handle_base&, istream&)
  239. { error_handler(1,"Read undefined");  }
  240.  
  241.  
  242. inline void   Init(handle_base&)            {}
  243. inline void   Clear(handle_base& y)         { y.clear(); }
  244. inline GenPtr Copy(handle_base& y)          { return y.copy();}
  245. inline GenPtr Convert(const handle_base& y) { return y.PTR; }
  246.  
  247.  
  248.  
  249.  
  250. #define LEDA_HANDLE_TYPE(type)\
  251. inline void   Clear(type& y)  { y.clear(); }\
  252. inline GenPtr Copy(type& y)   { return y.copy();}\
  253. inline char*  Type_Name(const type*) { return STRINGIZE(type); }
  254.  
  255.  
  256.  
  257. //------------------------------------------------------------------------------
  258. // compare, Print, Read, Init, Copy, Clear, Convert, ...  for built-in types
  259. //------------------------------------------------------------------------------
  260.  
  261. // defining a linear order
  262.  
  263. inline int compare(const GenPtr& x, const GenPtr& y) 
  264. { return (char*)x-(char*)y; }
  265.  
  266. inline int compare(const char& x, const char& y)     { return x-y; }
  267.  
  268. inline int compare(const int& x, const int& y)
  269. { if (x < y)  return -1; 
  270.   else if (x > y)  return  1; 
  271.   else return 0;
  272. }
  273.  
  274. inline int compare(const long& x, const long& y)
  275. { if (x < y)  return -1; 
  276.   else if (x > y)  return  1; 
  277.   else return 0;
  278. }
  279.  
  280. inline int compare(const double& x, const double& y)
  281. { if (x < y)  return -1; 
  282.   else if (x > y)  return  1; 
  283.   else return 0;
  284. }
  285.  
  286. inline int compare(const float& x, const float& y)
  287. { if (x < y)  return -1; 
  288.   else if (x > y)  return  1; 
  289.   else return 0;
  290. }
  291.  
  292.  
  293. // stream output
  294.  
  295. inline void Print(const GenPtr& x, ostream& out = cout) { out << x; }
  296. inline void Print(const char& x, ostream& out = cout)   { out << x; }
  297. inline void Print(const short& x, ostream& out = cout)  { out << x; }
  298. inline void Print(const int& x, ostream& out = cout)    { out << x; }
  299. inline void Print(const long& x, ostream& out = cout)   { out << x; }
  300. inline void Print(const float& x, ostream& out = cout)  { out << x; }
  301. inline void Print(const double& x, ostream& out = cout) { out << x; }
  302.  
  303. // stream input
  304.  
  305. inline istream& operator>>(istream& in, GenPtr)     { return in; }
  306.  
  307. inline void Read(GenPtr,   istream&)          {}
  308. inline void Read(char& x,  istream& in = cin) { in >> x; }
  309. inline void Read(short& x, istream& in = cin) { in >> x; }
  310. inline void Read(int&  x,  istream& in = cin) { in >> x; }
  311. inline void Read(long& x,  istream& in = cin) { in >> x; }
  312. inline void Read(float& x, istream& in = cin) { in >> x;}
  313. inline void Read(double& x,istream& in = cin) { in >> x;}
  314.  
  315.  
  316. // initialization
  317.  
  318. inline void Init(const GenPtr&) {}
  319. inline void Init(char&   x) { x=0; }
  320. inline void Init(short&  x) { x=0; }
  321. inline void Init(int&    x) { x=0; }
  322. inline void Init(long&   x) { x=0; }
  323. inline void Init(float&  x) { x=0; }
  324. inline void Init(double& x) { x=0; }
  325.  
  326.  
  327. // destruction
  328.  
  329. inline void Clear(const GenPtr&) {}
  330. inline void Clear(char&  )    {}
  331. inline void Clear(short& )    {}
  332. inline void Clear(int&   )    {}
  333. inline void Clear(long&  )    {}
  334. inline void Clear(float& )    {}
  335. inline void Clear(double& x) { deallocate_bytes(&x,sizeof(double)); }
  336.  
  337.  
  338. // copying
  339.  
  340. inline GenPtr Copy(GenPtr x)  { return x; }
  341. inline GenPtr Copy(char x)    { GenPtr p; *((char*)&p) =x; return p; }
  342. inline GenPtr Copy(short x)   { GenPtr p; *((short*)&p)=x; return p; }
  343. inline GenPtr Copy(int  x)    { return *(GenPtr*)&x; }
  344. inline GenPtr Copy(long x)    { return *(GenPtr*)&x; }
  345. inline GenPtr Copy(float x)   { return *(GenPtr*)&x; }
  346. inline GenPtr Copy(double x)
  347. { double* p = (double*)allocate_bytes(sizeof(double));
  348.   *p = x;
  349.   return p;
  350.  }
  351.  
  352.  
  353. // converting to a generic pointer
  354.  
  355. inline GenPtr Convert(GenPtr x)     { return x; }
  356. inline GenPtr Convert(char  x)      { GenPtr p; *((char*)&p) =x; return p; }
  357. inline GenPtr Convert(short x)      { GenPtr p; *((short*)&p)=x; return p; }
  358. inline GenPtr Convert(int   x)      { return GenPtr(x); }
  359. inline GenPtr Convert(long  x)      { return GenPtr(x); }
  360. inline GenPtr Convert(float x)      { return *(GenPtr*)&x; }
  361. inline GenPtr Convert(double& x)    { return GenPtr(&x); }
  362.  
  363.  
  364.  
  365. #if !defined(__TEMPLATE_FUNCTIONS__)
  366.  
  367. // access through a generic pointer
  368.  
  369. inline const GenPtr& Access(const GenPtr, const GenPtr& p) { return p; }
  370. inline double& Access(const double*, GenPtr p){ return *(double*)p;  }
  371.  
  372. #endif
  373.  
  374.  
  375. // integer type or not ?
  376.  
  377. inline int Int_Type(GenPtr) { return 0; }
  378. inline int Int_Type(const int*)   { return 1; }
  379. inline int Int_Type(const long*)  { return 1; }
  380.  
  381.  
  382. // name of types
  383.  
  384. inline char* Type_Name(const GenPtr)     { return "unknown"; }
  385. inline char* Type_Name(const char*  )    { return "char"; }
  386. inline char* Type_Name(const short* )    { return "short"; }
  387. inline char* Type_Name(const int*   )    { return "int"; }
  388. inline char* Type_Name(const long*  )    { return "long"; }
  389. inline char* Type_Name(const float* )    { return "float"; }
  390. inline char* Type_Name(const double*)    { return "double"; }
  391.  
  392.  
  393. #if defined(__BUILTIN_BOOL__)
  394. inline GenPtr Convert(bool  x)        { GenPtr p; *((bool*)&p)=x; return p; }
  395. inline GenPtr Copy(bool  x)           { GenPtr p; *((bool*)&p)=x; return p; }
  396. inline void   Clear(bool& x)          { x=false; }
  397. inline bool&  Access(const bool*, const GenPtr& p) { return *(bool*)&p; }
  398. inline void   Init(bool& x)           { x=false; }
  399. inline char*  Type_Name(const bool*)  { return "bool"; }
  400.  
  401. inline int  compare(const bool& x, const bool& y) { return char(x)-char(y); }
  402. inline void Print(const bool& x, ostream& out) { out << (x ? "true":"false"); }
  403. inline void Read(bool&,  istream&) {}
  404. #endif
  405.  
  406.  
  407.  
  408. // maximal and minimal values for some numerical types
  409.  
  410. inline int    Max_Value(int& x)     { return x =  MAXINT;   }
  411. inline int    Min_Value(int& x)     { return x = -MAXINT;   }
  412. inline float  Max_Value(float& x)   { return x =  MAXFLOAT; }
  413. inline float  Min_Value(float& x)   { return x = -MAXFLOAT; }
  414. inline double Max_Value(double& x)  { return x =  MAXDOUBLE;}
  415. inline double Min_Value(double& x)  { return x = -MAXDOUBLE;}
  416.  
  417.  
  418. /*
  419.   type arguments for the LEDA_TYPE_PARAMETER macro must define:
  420.  
  421.   a constructor taking no arguments: type::type()
  422.   a copy constructor:                type::type(const type&)
  423.   a Read function:                   void Read(type&, istream&)
  424.   a Print function:                  void Print(const type&, ostream&)
  425.   a compare function:                int compare(const type&, const type&)
  426.  
  427. */
  428.  
  429. #define LEDA_TYPE_PARAMETER(type)\
  430. inline void   Init(type&)                        {}\
  431. inline void   Clear(type& x)                     { delete (type*)&x; }\
  432. inline GenPtr Copy(type& x)                      { return new type(x);}\
  433. inline GenPtr Convert(type& x)                   { return GenPtr(&x);}\
  434. inline type&  Access(const type*, GenPtr p)      { return *(type*)p; }\
  435. inline char*  Type_Name(const type*)             { return STRINGIZE(type); }
  436.  
  437.  
  438. #define DEFINE_LINEAR_ORDER(type,cmp,new_type)\
  439. struct new_type : public type\
  440. { new_type(type s)            : type(s) {}\
  441.   new_type(const new_type& s) : type(s) {}\
  442.   new_type() {}\
  443.  ~new_type() {}\
  444. };\
  445. inline int compare(const new_type& x, const new_type& y) { return cmp(x,y); }
  446.  
  447.  
  448. //------------------------------------------------------------------------------
  449. // Macros
  450. //------------------------------------------------------------------------------
  451.  
  452. // nil pointer
  453.  
  454. #define nil 0
  455.  
  456.  
  457. // ACCESS(T,x): access a reference of type T through a generic pointer p
  458. // we have to create a dummy first argument of type T* for Access(T*,x)
  459.  
  460.  
  461.  
  462. #if defined(__TEMPLATE_FUNCTIONS__)
  463. #define ACCESS(type,x)  Access((type*)0,x)
  464. #else
  465. #define ACCESS(type,x)  (type&)(Access((type*)0,x))
  466. #endif
  467.  
  468.  
  469. #define INT_TYPE(type)  Int_Type((type*)0)
  470. #define TYPE_NAME(type) Type_Name((type*)0)
  471.  
  472.  
  473. //turning a macro argument into a string
  474.  
  475. #if defined(__STDC__)  || defined(__MSDOS__)
  476. #define STRINGIZE(x) #x
  477. #else
  478. #define STRINGIZE(x) "x"
  479. #endif
  480.  
  481.  
  482. // ITERATION MACROS
  483.  
  484. #define forall_items(x,S) for(x = (S).first_item(); x; x = (S).next_item(x) )
  485.  
  486.  
  487.  
  488.  
  489. // To avoid multiple declarations of the "forall_loop_item" variable
  490. // we use a dummy for-loop to create a new block for it.
  491. // This, however, does not work with all compilers (ztc,lucid ...)
  492. // and we have to use the old forall mechanism (no nesting).
  493.  
  494. #if defined(__ZTC__) || defined(__lucid)
  495.  
  496. #define forall(x,S)\
  497. for((S).start_iteration(); (S).read_iterator(x); (S).move_to_succ())
  498.  
  499. #define Forall(x,S)\
  500. for((S).Start_iteration(); (S).read_iterator(x); (S).move_to_pred())
  501.  
  502. #else
  503.  
  504. #define forall(x,S)\
  505. for(LEDA::loop_dummy=0; LEDA::loop_dummy<1; LEDA::loop_dummy++)\
  506. for(GenPtr forall_loop_item=(S).first_item();\
  507. (S).forall_loop_test(forall_loop_item,x);\
  508. (S).loop_to_succ(forall_loop_item))
  509.  
  510. #define Forall(x,S)\
  511. for(LEDA::loop_dummy=0; LEDA::loop_dummy<1; LEDA::loop_dummy++)\
  512. for(GenPtr forall_loop_item=(S).last_item();\
  513. (S).forall_loop_test(forall_loop_item,x);\
  514. (S).loop_to_pred(forall_loop_item))
  515.  
  516. #endif
  517.  
  518.  
  519.  
  520.  
  521.  
  522. // miscellaneous
  523.  
  524. #define Main            main(int argc, char** argv)
  525. #define newline         cout << endl
  526. #define forever         for(;;)
  527. #define loop(a,b,c)     for (a=b;a<=c;a++)
  528.  
  529. #define Max(a,b) ( (a>b) ? a : b )
  530. #define Min(a,b) ( (a>b) ? b : a )
  531.  
  532.  
  533.  
  534. //------------------------------------------------------------------------------
  535. // enumerations
  536. //------------------------------------------------------------------------------
  537.  
  538. enum rel_pos { before = 1, after = 0 };
  539.  
  540. enum direction { forward = 0, backward = 1 };
  541.  
  542.  
  543. //------------------------------------------------------------------------------
  544. // boolean
  545. //------------------------------------------------------------------------------
  546.  
  547. #if !defined(__BUILTIN_BOOL__)
  548.  
  549. typedef char bool;
  550.  
  551. enum {false=0, true=1};
  552.  
  553. #endif
  554.  
  555.  
  556.  
  557. //------------------------------------------------------------------------------
  558. // strings
  559. //------------------------------------------------------------------------------
  560.  
  561. class string_rep : public handle_rep {
  562.  
  563. friend class string;
  564.  
  565.       char*   s;
  566.  
  567. int dummy;
  568.  
  569.  string_rep(const char*);
  570.  string_rep(char);
  571.  
  572. ~string_rep() { delete[] s; }
  573.  
  574.  LEDA_MEMORY(string_rep)
  575.  
  576. };
  577.  
  578.  
  579. class format_string  // used in string constructor (see below)
  580. { friend class string;
  581.   const char* str;
  582.   public:
  583.   format_string(const char* s) { str = s; }
  584. };
  585.  
  586.  
  587. class string  : public handle_base
  588. {
  589.  
  590.  friend class string_rep;
  591.  
  592.  friend class panel;
  593.  
  594.  static char* str_dup(const char*);
  595.  static char* str_cat(const char*,const char*);
  596.  static char* str_ncat(int, char**);
  597.  
  598.  string_rep*  ptr() const { return (string_rep*)PTR; }
  599.  
  600.  char** access_ptr() { return &(ptr()->s); }   // used by panel::string_item
  601.  
  602. public:
  603.  
  604.  string()                { PTR = new string_rep(""); }
  605.  string(char c)          { PTR = new string_rep(c);  }
  606.  
  607. //
  608. // string(const char*);
  609. // string(const char*, ...); // printf-like constructor
  610. //
  611. // That's what we want, but then (ARM page 326) a call string("xyz") is
  612. // ambiguous. We use the dummy class "format_string" to resolve  the
  613. // ambiguity:
  614. //
  615. // string(const char*);
  616. // string(format_string, ...);
  617. //
  618. // However, cfront's stdarg (on sparcs) cannot handle the case correctly
  619. // where the first argument is a class object (like format_string). In this
  620. // case we define constructors for each possible second argument and
  621. // we end up with :
  622.  
  623.  string(const char* s)       { PTR = new string_rep(s);}
  624.  
  625.  
  626. #if defined(__GNUG__) || defined(__lucid) || !defined(sparc)
  627.  string(format_string, ...);
  628. #else
  629.  string(const char*, int,   ...);
  630.  string(const char*, long,  ...);
  631.  string(const char*, float, ...);
  632.  string(const char*, double,...);
  633.  string(const char*, char*, ...);
  634.  string(const char*, void*, ...);
  635. #endif
  636.  
  637.  
  638.  string(int argc, char** argv) { PTR = new string_rep(str_ncat(argc,argv)); }
  639.  
  640.  string(const string& x) : handle_base(x)  {}
  641.  
  642. ~string() { clear(); }
  643.  
  644. string& operator=(const string& x) { handle_base::operator=(x); return *this; }
  645.  
  646. char*    operator~()   const { return str_dup(ptr()->s); }   // makes a copy !
  647. char*    cstring()     const { return ptr()->s; }
  648. operator const char*() const { return ptr()->s; }
  649.  
  650. int    length()          const;
  651. string sub(int,int)      const;
  652. int    pos(string,int=0) const;
  653.  
  654. string operator()(int i, int j)  const { return sub(i,j); }
  655. string head(int i)               const { return sub(0,i-1); }
  656. string tail(int i)               const { return sub(length()-i,length()-1); }
  657.  
  658. string insert(string, int=0)     const;
  659. string insert(int, string)       const;
  660.  
  661. string replace(const string&, const string&, int=1) const;
  662. string replace(int, int, const string&) const;
  663.  
  664. string del(const string&, int=1) const;
  665. string del(int, int) const;
  666.  
  667. void   read(istream&, char delim=' ');
  668. void   read(char delim=' ')           { read(cin,delim); }
  669. void   read_line(istream& I = cin)    { read(I,'\n'); }
  670.  
  671. string replace_all(const string& s1, const string& s2)
  672.                                          const  { return replace(s1,s2,0); }
  673. string replace(int i, const string& s)   const  { return replace(i,i,s);  }
  674.  
  675. string del_all(const string& s)          const  { return del(s,0); }
  676. string del(int i)                        const  { return del(i,i); } 
  677.  
  678. string format(string) const;
  679.  
  680. char  operator[](int) const;
  681. char& operator[](int);
  682.  
  683. string  operator+(const string& x)  const;
  684. string  operator+(const char* x)  const;
  685. string& operator+=(const string& x);
  686.  
  687. friend int operator==(const string& x, const string& y);
  688. friend int operator==(const string& x, const char* y);
  689. friend int operator!=(const string& x, const string& y);
  690. friend int operator!=(const string& x, const char* y);
  691. friend int operator< (const string& x, const string& y);
  692. friend int operator> (const string& x, const string& y);
  693. friend int operator<=(const string& x, const string& y);
  694. friend int operator>=(const string& x, const string& y);
  695.  
  696. friend istream& operator>>(istream&, string&);
  697. friend ostream& operator<<(ostream&, const string&) ;
  698.  
  699. };
  700.  
  701. inline void Print(const string& x, ostream& out)      { out << x; }
  702. inline void Read(string& x, istream& in)              { in  >> x; }
  703. extern int compare(const string& x, const string& y);
  704.  
  705. LEDA_HANDLE_TYPE(string)
  706.  
  707.  
  708.  
  709. //------------------------------------------------------------------------------
  710. // INT<cmp>: int with user defined linear order cmp
  711. //------------------------------------------------------------------------------
  712.  
  713. typedef int   (*CMP_INT_TYPE)(int,int);
  714.  
  715.  
  716. template<CMP_INT_TYPE cmp>
  717.  
  718. class _CLASSTYPE INT
  719. {int p;
  720.  
  721. public:
  722.  INT(const int i=0) { p = i;}
  723.  operator int()     { return p; }
  724.  
  725. FRIEND_INLINE void Print(const INT<cmp>& x, ostream& out = cout) { out << x.p; }
  726. FRIEND_INLINE void Read(INT<cmp>&  x, istream& in = cin)  { in  >> x.p; }
  727. FRIEND_INLINE void Init(INT<cmp>&  x)            { x.p=0; }
  728. FRIEND_INLINE GenPtr Copy(const INT<cmp>& x)     { return GenPtr(x); }
  729. FRIEND_INLINE void Clear(INT<cmp>& x)            { x.p = 0; }
  730. FRIEND_INLINE GenPtr Convert(const INT<cmp>& x)  { return GenPtr(x.p); }
  731. FRIEND_INLINE INT<cmp>& Access(INT<cmp>*,const GenPtr& p) { return *(INT<cmp>*)&p; }
  732.  
  733. FRIEND_INLINE int compare(const INT<cmp>& x, const INT<cmp>& y) { return cmp(x.p,y.p);}
  734. };
  735.  
  736.  
  737.  
  738. //------------------------------------------------------------------------------
  739. // random numbers, timing, etc.
  740. //------------------------------------------------------------------------------
  741.  
  742. #if defined(random)
  743. // if random is a macro undefine it
  744. #undef random
  745. #endif
  746.  
  747.  
  748. #if defined (__NO_RANDOM__)
  749. // the following two functions are defined in basic/_random.c
  750. extern int leda_random();
  751. extern void leda_srandom(int);
  752. inline int random()  { return leda_random(); }
  753. inline void srandom(int s) { leda_srandom(s); }
  754. #endif
  755.  
  756. inline int      random(int a, int b)  { return a + random()%(b-a+1); }
  757. inline double   rrandom() { return double(random())/MAXINT; }
  758. inline unsigned urandom(unsigned a, unsigned b) 
  759. { return a + (unsigned)(random())%(b-a+1); }
  760.  
  761. extern void init_random(int=0);
  762.  
  763. extern float used_time();
  764. extern float used_time(float&);
  765. extern void  print_time(string s);
  766. inline void  print_time() { print_time(""); }
  767. extern void  wait(unsigned int seconds);
  768.  
  769.  
  770. typedef int (*LEDA_SIG_PF) (...);
  771.  
  772. extern LEDA_SIG_PF catch_interrupts(LEDA_SIG_PF handler = nil);
  773.  
  774.  
  775. //------------------------------------------------------------------------------
  776. // input/output
  777. //------------------------------------------------------------------------------
  778.  
  779. extern int    Yes(string);
  780. extern int    Yes();
  781.  
  782. extern int    read_int(string);
  783. extern int    read_int();
  784.  
  785. extern char   read_char(string);
  786. extern char   read_char();
  787.  
  788. extern double read_real(string);
  789. extern double read_real();
  790.  
  791. extern string read_line(istream& =cin);
  792. extern string read_string(string);
  793. extern string read_string();
  794.  
  795. extern void skip_line(istream& =cin);
  796.  
  797. #endif
  798.